package com.lq.controller;

import com.lq.pojo.Log;
import com.lq.pojo.User;
import com.lq.service.ArticleService;
import com.lq.service.CommentService;
import com.lq.service.DocumentService;
import com.lq.service.LinkService;
import com.lq.util.SystemLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class PanelController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LinkService linkService;

    @Autowired
    private DocumentService documentService;

    @RequestMapping("/panel")
    public String panel(HttpSession session, Model model) {

        User user = (User) session.getAttribute("user");

        int articleNum = articleService.getNumOfPublishArticle(user.getUserId());
        int commentNum = commentService.getNumOfAllCommentByUserId(user.getUserId());
        int linkNum = linkService.getNumsOfLinkByUserId(user.getUserId());
        int documentNum = documentService.getNumsOfDocumentByUserId(user.getUserId());

        model.addAttribute("articleNum", articleNum);
        model.addAttribute("commentNum", commentNum);
        model.addAttribute("linkNum", linkNum);
        model.addAttribute("documentNum", documentNum);

        SystemLogUtil.getSystemLog();
        return "forward:/getLatestArticle";
    }

    @RequestMapping("/getSystemLog")
    public String getSystemLog(Model model) {

        List<Log> logList = SystemLogUtil.getSystemLog();
        model.addAttribute("logList", logList);

        return "panel";
    }
}
